<template>
	<view class="max-bg-white u-p-40">
		<u-image width="218.8rpx" height="76rpx" src="http://img.maxhuangman.com/lvdi/logo.png"
			style="visibility: hidden;"></u-image>
		<view class="u-flex-col u-m-t-80">
			<open-data type="userAvatarUrl" class="userAvatarUrl"></open-data>
			<open-data type="userNickName" class="userNickName"></open-data>
			<!-- <button type="primary" open-type="getUserInfo" lang="zh_CN" @getuserinfo="wechatLogin">微信登录授权</button> -->
			<button type="primary" open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber">获取手机号</button>
			<!-- <button type="primary"  @click="appWechatLogin('1',{})">获取手机号</button> -->
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				targetUrl: null,
				code: ''
			};
		},
		onLoad(option) {
			let targetUrl = option.targetUrl;
			console.log("返回地址：" + targetUrl);
			this.targetUrl = targetUrl ? targetUrl : '/pages/index'
			//微信登录
			uni.login({
				provider: 'weixin',
				success: (res2) => {
					this.code = res2.code;
					// uni.showToast({
					// 	title: res2.code,
					// 	icon: "none"
					// });
					console.log(this.code)
				},
				fail: () => {
					uni.showToast({
						title: "微信登录授权失败",
						icon: "none"
					});
				}
			})
		},
		methods: {

			appWechatLogin(code, wechatRet) {
				this.$u.post('app/wechat/login', {
					code: code,
					encryptedData: wechatRet.detail.encryptedData,
					iv: wechatRet.detail.iv
					// encryptedData: '1',
					// iv: '1'
				}).then(ret => {
					console.log(ret);
					uni.$u.vuex('vuex_token', ret.token);
					uni.setStorageSync("expires_in", ret.expire);
					uni.setStorageSync("last_login_time", new Date().getTime());
					if (this.targetUrl == '/pages/project/index' ||
						this.targetUrl == '/pages/service/index' ||
						this.targetUrl == '/pages/lease/index' ||
						this.targetUrl == '/pages/articles/index') {
						this.$u.route({
							url: this.targetUrl,
							type: 'switchTab'
						})
					} else {
						uni.$u.route(this.targetUrl);
					}
				});
			},

			onGetPhoneNumber(e) {
				if (e.detail.errMsg == "getPhoneNumber:fail user deny") { //用户决绝授权  
					//拒绝授权后弹出一些提示  
				} else { //允许授权  
					console.log(e.detail.encryptedData)
					console.log(e.detail.iv)
					// e.detail.encryptedData //加密的用户信息  
					// e.detail.iv //加密算法的初始向量  时要用到  
					this.appWechatLogin(this.code, e);
				}
			},
			wechatLogin() {
				uni.$u.route(this.targetUrl);
				return
				// 11111#1
				let that = this;
				// #ifdef MP-WEIXIN
				uni.getProvider({
					service: 'oauth',
					success: function(res) {
						if (~res.provider.indexOf('weixin')) {
							uni.login({
								provider: 'weixin',
								success: (res2) => {
									uni.getUserInfo({
										provider: 'weixin',
										success: (info) => { //这里请求接口
											console.log(info);
											// that.$u.post('wechat/login', {
											// 	code: res2.code,
											// 	encryptedData: info.encryptedData,
											// 	iv: info.iv
											// }).then(ret => {
											// 	console.log(ret);
											// 	uni.$u.vuex('vuex_token', ret.token);
											// 	uni.setStorageSync("expires_in", ret.expires_in);
											// 	uni.setStorageSync("last_login_time", new Date().getTime());
											// 	if (this.targetUrl == '/pages/project/index' ||
											// 		this.targetUrl == '/pages/service/index' ||
											// 		this.targetUrl == '/pages/lease/index' ||
											// 		this.targetUrl == '/pages/articles/index') {
											// 		this.$u.route({
											// 			url: this.targetUrl,
											// 			type: 'switchTab'
											// 		})
											// 	} else {
											// 		uni.$u.route(this.targetUrl);
											// 	}
											// });
										},
										fail: () => {
											uni.showToast({
												title: "微信登录授权失败",
												icon: "none"
											});
										}
									})

								},
								fail: () => {
									uni.showToast({
										title: "微信登录授权失败",
										icon: "none"
									});
								}
							})

						} else {
							uni.showToast({
								title: '请先安装微信或升级版本',
								icon: "none"
							});
						}
					}
				});
				// #endif
			}
		}
	};
</script>

<style>
	.userAvatarUrl {
		width: 196rpx;
		height: 196rpx;
		flex-direction: row;
		justify-content: center;
		display: block;
		margin: 0 auto;
		border-radius: 50%;
		overflow: hidden;
	}

	.userNickName {
		display: flex;
		flex-direction: row;
		justify-content: center;
		color: #303133;
		font-size: 28rpx;
		margin: 40rpx;
		margin-bottom: 340rpx;
	}

	button {
		position: relative;
		display: block;
		box-sizing: border-box;
		font-size: 32rpx;
		text-align: center;
		text-decoration: none;
		color: #333333;
		line-height: 2.55555556;
		border-radius: 8rpx;
		-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
		overflow: hidden;
		background-color: #fff;
		width: 560rpx;
		height: 88rpx;
		margin: 0 auto;
		border: 1rpx solid #CCCCCC;
	}
</style>
